<?php

namespace Common\Repository;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class RedisRepository extends Controller
{
    public $redisObject;

    public function __construct()
    {
        $this->redisObject = new Redis();
    }

    /**
     * 判断键值是否存在
     *
     * @param string $key
     * @return bool
     */
    public final function exists(string $key)
    {
        if (!$this->redisObject->exists($key)) {
            return false;
        }
        return true;
    }

    /**
     * 时间戳生命周期
     *
     * @param string $key
     * @param string $expireat
     * @return bool
     */
    public final function expireat(string $key, string $expireat)
    {
        if (!$key && !$expireat) {
            return false;
        }
        return $this->redisObject->expireat($key, $expireat);
    }

    /* @name 毫秒生命周期
     * @param string $key 键值
     * @param string $pexpireat 过期时间 以毫米为单位
     * @return bool
     */
    public function pexpireat(string $key, string $pexpireat)
    {
        if (!$key && $pexpireat) {
            return false;
        }
        return $this->redisObject->pexpireat($key, $pexpireat);
    }

    /* @name Redis 设置锁
     * @param string $key 键值
     * @param string $value 值
     * @param string $expireat 键值的生命周期
     * @return bool
     */
    public final function setnx(string $key, string $value, string $expireat = '')
    {
        if (!$key && !$value) {
            return false;
        }
        $Resources = $this->redisObject->setnx($key, $value);
        if ($expireat && strlen($expireat) == 10) {
            return $this->expireat($key, $expireat);// 时间戳生命周期
        } else if ($expireat && strlen($expireat) > 10) {
            return $this->pexpireat($key, $expireat);//毫秒生命周期
        }
        return $Resources;
    }

    /* @name 批量查询特地字符 方法少用影响性能
     * @param string $key
     * @return object
     */
    public final function keys(string $pattern = '*')
    {
        if (!$pattern) {
            return false;
        }
        return $this->redisObject->keys($pattern);
    }

    /* @name 返回键值剩余过期时间
     * @param string $key
     * @return integer|array -2 键值不存在 -1键值没有剩余时间
     */
    public final function pttl(string $key)
    {
        return $this->redisObject->pttl($key);
    }

    /* @name 删除键值
     * @param array|string $keys
     * @return bool
     */
    public function delkey($keys)
    {
        if (!$keys) {
            return false;
        }
        return $this->redisObject->del($keys);
    }

    /* @name 移除单个元素
     * @param string $key
     * @param string $val
     */
    public function zrem(string $key, string $val)
    {
        if (!$key && !$val) {
            return false;
        }
        return $this->redisObject->zrem($key, $val);
    }

    /* @name 设置一个值string 类型
     * @param string $key
     * @param string $val
     */
    public function set(string $key, string $value, $expireResolution = null, $expireTTL = null, $flag = null)
    {
        if (!$key && !$value) {
            return false;
        }
        return $this->redisObject->set($key, $value, $expireResolution, $expireTTL, $flag);
    }
}